package hongcaosp.app.android.comment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import hongcaosp.app.android.common.PresenterListener;
import hongcaosp.app.android.common.TestConstants;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.bean.Comment;
import hongcaosp.app.android.modle.bean.CommentReply;
import hongcaosp.app.android.modle.bean.CommentWrapper;
import hongcaosp.app.android.modle.bean.IComment;
import hongcaosp.app.android.modle.bean.IUser;
import hongcaosp.app.android.modle.bean.Pagebean;
import hongcaosp.app.android.modle.impl.CommentModleImpl;
import hongcaosp.app.android.modle.mi.CommentModle;
import hongcaosp.app.android.user.TotalCallBack;
import xlj.lib.android.base.component.BasePresenter;

public class CommentPresenter extends BasePresenter {
    private ICommentListView listView;

    private CommentModle commentModle;

    private int videoId;

    private int number;

    private int total;

    private Map<Comment, CommentRange> rangeMap = new HashMap<>();

    private List<IViewType> commentArray = new ArrayList<>();

    public CommentPresenter(ICommentListView listView, int videoId) {
        this.listView = listView;
        this.videoId = videoId;
        commentModle = new CommentModleImpl();
    }

    public void freshComments(final TotalCallBack totalCallBack) {
        number = 1;
        commentModle.videoComments(UserToken.getDefault().getToken(), videoId, 20, number, new DataCallBack<Pagebean<Comment>>() {
            @Override
            public void onGetData(Pagebean<Comment> data) {
                super.onGetData(data);
                boolean loadMore = false;
                total = data.getList() == null ? 0 : data.getList().size();
                if (data.getTotal() > total) {
                    loadMore = true;
                }
                List<Comment> comments = data.getList();
                List<IViewType> commentList = new ArrayList<>();
                for (Comment c : comments) {
                    CommentRange cr = new CommentRange();
                    cr.comment = c;
                    commentList.add(c);
                    if (c.getChildCommentsList() != null) {
                        for (CommentReply r : c.getChildCommentsList()) {
                            commentList.add(r);
                        }
                        cr.hasSize = c.getChildCommentsList().size();
                    }
                    cr.number = 0;
                    cr.total = c.getChildCommentsCount();
                    rangeMap.put(c, cr);
                }
                commentArray = commentList;
                listView.freshList(commentList, loadMore);
                if (totalCallBack != null) {
                    totalCallBack.onTotalSend(data.getTotal());
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                listView.freshList(null, false);
                if (totalCallBack != null) {
                    totalCallBack.onTotalSend(0);
                }
            }
        });
    }

    public void loadMoreComments() {
        number++;
        commentModle.videoComments(UserToken.getDefault().getToken(), videoId, 20, number, new DataCallBack<Pagebean<Comment>>() {
            @Override
            public void onGetData(Pagebean<Comment> data) {
                super.onGetData(data);
                boolean loadMore = false;
                total += data.getList() == null ? 0 : data.getList().size();
                if (data.getTotal() > total) {
                    loadMore = true;
                }
                List<Comment> comments = data.getList();
                List<IViewType> commentList = new ArrayList<>();
                for (Comment c : comments) {
                    commentList.add(c);
                    CommentRange cr = new CommentRange();
                    cr.comment = c;
                    if (c.getChildCommentsList() != null) {
                        for (CommentReply r : c.getChildCommentsList()) {
                            commentList.add(r);
                        }
                        cr.hasSize = c.getChildCommentsList().size();
                    }
                    cr.number = 0;
                    cr.total = c.getChildCommentsCount();
                    commentList.add(cr);
                    rangeMap.put(c, cr);
                }
                listView.addList(commentList, loadMore);
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                listView.addList(null, false);
            }
        });
    }

    public void loadMoreReply(Comment comment) {
        final CommentRange range = rangeMap.get(comment);
        int number = range.number;
        number++;
        range.number = number;
        commentModle.videoCommentsChildren(UserToken.getDefault().getToken(), comment.getId(), 5, number,
                new DataCallBack<Pagebean<CommentReply>>() {
                    @Override
                    public void onGetData(Pagebean<CommentReply> data) {
                        super.onGetData(data);
                        int index = commentArray.indexOf(range);
                        int end = index;
                        if (data.getList() != null) {
                            commentArray.addAll(index, data.getList());
                            range.hasSize += data.getList().size();
                            end += data.getList().size();
                        }
                        listView.moreReply(index, end);
                    }

                    @Override
                    public void onFail(int code, String errorMessage) {
                        super.onFail(code, errorMessage);
                        listView.notifyLoadFail(errorMessage);
                    }
                });
    }

    public void comments(String content, final PresenterListener listener) {
        commentModle.commentVideo(UserToken.getDefault().getToken(), 0, videoId, content, new DataCallBack<CommentWrapper<Comment>>() {
            @Override
            public void onGetData(CommentWrapper<Comment> data) {
                super.onGetData(data);
                commentArray.add(0,data.getComments());
                listView.moreReply(0,1);
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                if (listener != null) {
                    listener.onFail(errorMessage);
                }
            }
        });
    }

    public void reply(String content, final Comment comment, final PresenterListener listener) {
        commentModle.replyComments(UserToken.getDefault().getToken(), 0, videoId, comment.getId(), content,
                new DataCallBack<CommentWrapper<CommentReply>>() {
            @Override
            public void onGetData(CommentWrapper<CommentReply> data) {
                super.onGetData(data);
                int index = commentArray.indexOf(comment);
                commentArray.add(index + 1,data.getComments());
                listView.moreReply(index + 1,index + 2);
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                if (listener != null) {
                    listener.onFail(errorMessage);
                }
            }
        });
    }

    public void reply(String content,final CommentReply comment,final PresenterListener listener) {
        commentModle.replyComments(UserToken.getDefault().getToken(), 0, videoId, comment.getId(), content,
                new DataCallBack<CommentWrapper<CommentReply>>() {
                    @Override
                    public void onGetData(CommentWrapper<CommentReply> data) {
                        super.onGetData(data);
                        int index = commentArray.indexOf(comment);
                        commentArray.add(index + 1,data.getComments());
                        listView.moreReply(index + 1,index + 2);
                        if (listener != null) {
                            listener.onSuccess();
                        }
                    }

                    @Override
                    public void onFail(int code, String errorMessage) {
                        super.onFail(code, errorMessage);
                        if (listener != null) {
                            listener.onFail(errorMessage);
                        }
                    }
                });
    }

    public void good(final IComment comment, final PresenterListener listener) {
        commentModle.likeComment(UserToken.getDefault().getToken(), comment.getId(), new DataCallBack() {
            @Override
            public void onGetData(Object data) {
                super.onGetData(data);
                comment.setLike(true);
                comment.setLikeCounts(comment.getLikeCounts() + 1);
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                if (listener != null) {
                    listener.onFail(errorMessage);
                }
            }
        });
    }

    public void cancelGood(final IComment comment, final PresenterListener listener){
        commentModle.likeCommentCancel(UserToken.getDefault().getToken(), comment.getId(), new DataCallBack() {
            @Override
            public void onGetData(Object data) {
                super.onGetData(data);
                comment.setLike(false);
                comment.setLikeCounts(comment.getLikeCounts() - 1);
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                if (listener != null) {
                    listener.onFail(errorMessage);
                }
            }
        });
    }
}
